package jea.machine.transverseprofileviewer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.util.Vector;

import javax.swing.JPanel;

import jea.machine.db.VacuumProfile;

public class PanelProfileViewerBySPSLocation extends JPanel
{
	private static final long serialVersionUID = 1L;
	private Graphics2D g2d;
	private Vector<VacuumProfile> vp = new Vector<VacuumProfile>();
	private int initialHeigth, initialWidth;
	private int call = 0;
	private FrameProfileViewerBySPSLocation parentFrame;
	
	public PanelProfileViewerBySPSLocation(FrameProfileViewerBySPSLocation parentFrame)
	{
		super();
		this.parentFrame = parentFrame;
		initialHeigth = this.getHeight();
		initialWidth  = this.getWidth();
	}
	
	private void initPaint(Graphics g)
	{
     g2d = (Graphics2D) g;
	 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);	
     // Dessine les axes
     g2d.setColor(Color.DARK_GRAY);
     g2d.setStroke(new BasicStroke(0.2f));
     g2d.drawLine(0, initialHeigth/2, initialWidth, initialHeigth/2);
     g2d.drawLine(initialWidth/2, 0, initialWidth/2, initialHeigth);
     g2d.setColor(Color.BLACK);
     g2d.setStroke(new BasicStroke(2.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
     
     // itere sur les vp presents
     //System.out.println("VPSIZE = "+vp.size());
     if ( (parentFrame.stackProfiles() == true) && (vp.size() > 0) ) // on empile ou pas les profiles dans le panel ?
      for(int i=0; i < vp.size(); i++)
      {
       drawProfile(vp.get(i));
      }
     if ( (parentFrame.stackProfiles() == false) && (vp.size() > 0) ) 
       drawProfile(vp.get(vp.size()-1));
    	 
    }

	// DESSINE LE PROFIL EN FONCTION DU NOMBRE DE POINTS
	private void drawProfile(VacuumProfile vp)
	{
	   
	   if ( vp == null ) // si pas de donnees en DB
	   {
		  drawNoData();
		  return;
	   }
	
       if ( vp.getPoints() < 1 ) return;
	   // Elipse/circle 
	   if ( vp.getPoints() == 1)
	   {
		g2d.drawOval(initialWidth/2-(int)vp.getX(0)/2,initialHeigth/2-(int)vp.getY(0)/2,(int)vp.getX(0),(int)vp.getY(0));
		drawMinMaxText(vp.getX(0), vp.getY(0) );
	   }

	   // Polygone
	   int offsetX = initialWidth/2;
	   int offsetY = initialHeigth/2;
	   double minX = 1000000, maxX = -1000000, minY = 1000000, maxY = -1000000;
	   //	 System.out.println("# POINTS = "+vp.getPoints());
	   if ( vp.getPoints() >= 3)
	   {
		 int i=0;
		 for(i=0; i < vp.getPoints(); i++)
		 {
			// recherche min, max
			if ( vp.getX(i) < minX ) minX = vp.getX(i);
			if ( vp.getX(i) > maxX ) maxX = vp.getX(i);
			if ( vp.getY(i) < minY ) minY = vp.getY(i);
			if ( vp.getY(i) > maxY ) maxY = vp.getY(i);			
			// dessine le profil
			if ( i >= 1 )
			{
				System.out.println("Line (i="+i+")" + ((int)vp.getX(i-1)) +","+ ((int)vp.getY(i-1)) +","+ ((int)vp.getX(i)) +","+ ((int)vp.getY(i)));
				g2d.drawLine(offsetX+(int)vp.getX(i-1), offsetY-(int)vp.getY(i-1), offsetX+(int)vp.getX(i), offsetY-(int)vp.getY(i));
			}	
		 }
		 // ferme le profil
		 g2d.drawLine(offsetX+(int)vp.getX(i-1), offsetY-(int)vp.getY(i-1), offsetX+(int)vp.getX(0), offsetY-(int)vp.getY(0));
		 drawMinMaxText(maxX-minX, maxY-minY);
	   }
	
	 //System.out.println("DESCRIPTION="+vp.getProfileDescription()+", DRAWING="+vp.getProfileDrawing());
	 drawDescriptionText(vp.getProfileDescription());
	 drawDrawingRef(vp.getProfileDrawing());
	   
	}
	
	private void drawMinMaxText(double maxWidth, double maxHeight)
	{
		FontRenderContext frc = g2d.getFontRenderContext();
		Font f = new Font("Helvetica",Font.PLAIN,13);
		String largeur = new String("Max Width "+maxWidth+" [mm]");
		String hauteur = new String("Max Height "+maxHeight+" [mm]");
		TextLayout txtLargeur = new TextLayout(largeur, f, frc);
		TextLayout txtHauteur = new TextLayout(hauteur, f, frc);
		g2d.setColor(Color.BLACK);
		txtLargeur.draw(g2d, 5, 45);
		txtHauteur.draw(g2d, 5, 65);
	}
	
	private void drawDescriptionText(String description)
	{
		FontRenderContext frc = g2d.getFontRenderContext();
		Font f = new Font("Helvetica",Font.ITALIC,13);
		TextLayout txtLargeur = new TextLayout(description, f, frc);
		g2d.setColor(Color.BLACK);
		txtLargeur.draw(g2d, 5, 20);	
	}
	
	private void drawDrawingRef(String drawingRef)
	{
		FontRenderContext frc = g2d.getFontRenderContext();
		Font f = new Font("Helvetica",Font.BOLD,12);
		TextLayout txtLargeur = new TextLayout(drawingRef, f, frc);
		g2d.setColor(Color.BLACK);
		txtLargeur.draw(g2d, 300, 45);	
	}
	
	private void drawNoData()
	{
		FontRenderContext frc = g2d.getFontRenderContext();
		Font f = new Font("Helvetica",Font.ITALIC,13);
		TextLayout txtLargeur = new TextLayout("NO DATA IN DATABASE", f, frc);
		g2d.setColor(Color.BLACK);
		txtLargeur.draw(g2d, 180, 20);		
	}
	
	
	public void paintProfile(VacuumProfile vp)
	{
		this.vp.add(vp);
		this.repaint();
	}
	
	@Override
    public void paintComponent(Graphics g)
    {
       super.paintComponent(g);
       if ( call == 0 )
       {
   		initialHeigth = this.getHeight();
		initialWidth  = this.getWidth();
       }
       call++;
       initPaint(g);
       //System.out.println("*** repainting PanelProfileViewer ***");
    }	
}
